from flask import Blueprint, json,request,jsonify
from db import db1
from comm import get_data
bp_course=Blueprint('course',__name__)
@bp_course.route('/Bumen',methods=['GET','POST','PUT','DELETE'])
def Bumen():
    if request.method=='POST':
        data=get_data()
        sql="insert into bumen values(0,'%s',%d)"%(data['name'],int(data['total']))
        db1.insert(sql)
        sql1="select * from bumen where id=%d"%(int(data['id']))
        res=db1.find(sql1)
        total=int(res['total'])+1
        sql2="update bumen set total=%d where id=%d"%(total,int(data['id']))
        db1.insert(sql2)
        return jsonify({'code':200})
    elif request.method=='GET':
        id=request.args.get('id')
        if id:
            sql="select * from bumen where id=%d"%(int(id))
            res=db1.find(sql)
            return jsonify({'data':res})
        else:
            sql="select * from bumen"
            res=db1.find(sql)
            return jsonify({"data":res})
    elif request.method=='PUT':
        data=request.form
        sql="update bumen set name='%s'"%(data['name'])
        db1.insert(sql)
        return jsonify({"code":200})
    elif request.method=='DELETE':
        id=request.args.get('id')
        sql="delete from bumen where id=%d"%(int(id))
        db1.insert(sql)
        return jsonify({"code":200})


@bp_course.route('/yuangon',methods=['GET','POST','PUT','DELETE'])
def yuangon():
    if request.method=='POST':
        data=get_data()
        sql="insert into yuangon values(0,'%s','%s',%d,%d)"%(data['name'],data['gender'],int(data['money']),int(data['bm']))
        db1.insert(sql)
        return jsonify({'code':200})

    elif request.method=='GET':
        id=request.args.get('id')
        if id:
            sql="select * from yuangon where id=%d"%(int(id))
            res=db1.find(sql)
            return jsonify({'data':res})
        else:
            sql="select * from yuangon"
            res=db1.find(sql)
            return jsonify({"data":res})
    elif request.method=='PUT':
        data=request.form
        sql="update yuangon set name='%s,gender='%s',money=%d,bm=%d"%(data['name'],data['gender'],int(data['money']),int(data['bm']))
        db1.insert(sql)
        return jsonify({"code":200})
    elif request.method=='DELETE':
        id=request.args.get('id')
        sql="delete from yuangon where id=%d"%(int(id))
        db1.insert(sql) 
        return jsonify({"code":200})

#景点操作接口
# @bp_course.route('/scenicm',methods=['POST','GET'])
# def scenicm():
#     if request.method == "POST":
#         #图片上传，vue封装formdata
#         data = request.form
#         #添加景点
#         sql = "insert into scenic(title,price,typeid) values('%s',%.2f,%d)"%(data['title'],float(data['price']),int(data['typeid']))
#         db1.insert(sql)
#         #获取新加的id
#         sql1 = "select id from scenic where title='%s'"%(data['title'])
#         res = db1.find(sql1)
#         #文件上传，上传成功后图片路径
#         file = request.files['file']
#         myfile.file_mkdir(str(res['id']))
#         file.save(os.path.join('./static/upload/'+str(res['id']),file.filename))
#         #组装数据添加
#         sql3 = "update scenic set pic='%s' where id=%d"%('/static/upload/'+str(res['id'])+"/"+file.filename,int(res['id']))
#         db1.insert(sql3)
#         return jsonify({"code":200})
#     else:
#         sql = 'select * from scenic'
#         res = db1.findall(sql)
#         return jsonify({"code":200,'list':res})


# import os
# from utils.myfile import myfile
# # 文件上传
# @bp_course.route('/upload',methods=['POST'])
# def upload():
#     # 获取文件
#     file=request.files['file']
#     # 选择文件夹
#     # 创建文件夹    
#     myfile.file_mkdir(3)

#     # 保存文件
#     file.save(os.path.join('./static/upload/3/',file.filename))
#     # 保存
#     return 'ok'
    



@bp_course.route('/newn',methods=['GET','POST'])
def newn():
    if request.method=='POST':
        data=get_data()
        sql="insert into new_n(id,title) values(0,'%s')"%(data['title'])
        db1.insert(sql)
        return jsonify({'code':200})
    else:
        id=request.args.get('id')
        if id:
            sql="select * from new_n where id=%d"%(int(id))
            res=db1.find(sql)
            return jsonify({'list':res})
        else:
            sql="select * from new_n"
            res=db1.find_all(sql)
            return jsonify({'list':res})


@bp_course.route('/newg',methods=['GET','POST'])
def newg():
    if request.method=='POST':
        data=get_data()
        sql="insert into table new_g(total,n_id) values('%s')"%(data['tatal'],data['n_id'])
        db1.insert(sql)
        return jsonify({'code':200})
    else:
        id=request.args.get('id')
        n_id=request.args.get('n_id')
        sql="select * from new_g where %d=%d"%(int(id),int(n_id))
        res=db1.find_all(sql)
        return jsonify({'list':res})



@bp_course.route('/red',methods=['GET','POST'])
def red():
    money=request.args.get("money")
    # 对数据有效性验证
    if not money:
        return jsonify({"code":10011,"mes":"必须输入金额"})
    else:
        try:
            # 对数据格式验证
            money=float(money)
        except:
            return jsonify({"code":10012,"msg":'格式有误'})
    if 0.01<money<200:
        return jsonify({'code':200})
    else:
        return jsonify({'code':10011})
